package cn.school.Servlet;

import cn.school.Entity.Admin;
import cn.school.Entity.Page;
import cn.school.Service.AdminService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.List;

@WebServlet("*.ao")
public class MainServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public MainServlet() {
		super();
	}

	AdminService a = new AdminService();
protected Page<Admin> Search(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
        AdminService a=new AdminService();
	int pageNo = Integer.parseInt(request.getParameter("page"));
	int pageSize=Integer.parseInt(request.getParameter("limit"));
	String tel= request.getParameter("tel");
	String sname=request.getParameter("sname");
         return a.pageing(pageNo,pageSize,tel,sname);
	}

	protected int add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	String name=request.getParameter("username");
		String tel=request.getParameter("tel");
		String pass=request.getParameter("pass");
		Admin a=new Admin(name,tel,pass,2);
		AdminService ad=new AdminService();
		return ad.add(a);
	}

	protected int update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		long id=Long.parseLong(request.getParameter("id"));
		String name=request.getParameter("username");
		String tel=request.getParameter("tel");
		String pass=request.getParameter("pass");
		int role=Integer.parseInt(request.getParameter("role"));
		Admin a=new Admin(id,name,tel,pass,role);
		AdminService ad=new AdminService();
return ad.up(a);
}

	protected int del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		long id=Long.parseLong(request.getParameter("id"));
		AdminService a=new AdminService();
		return a.del(id);
	}

	protected void admin(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		HttpSession h = request.getSession();
		AdminService a = new AdminService();
		try {
			Admin a1=a.admin(request.getParameter("root"), request.getParameter("pass"));
			if (a1!=null) {
				h.setAttribute("admin",a1);
				System.out.println(a1);
				if (a1.getRole()==1) {
					response.sendRedirect("index1.html");
					return;
				}if (a1.getRole()==2) {
					response.sendRedirect("vip-list.html");
					return;
				}
			}else{
				response.sendRedirect("erro.html");
				return;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");

		PrintWriter out = response.getWriter();
		String path = request.getServletPath();
		path = path.substring(1, path.length() - 3);
		if (path.equals("doGet") || path.equals("doPost") || path.equals("service")) {
			response.sendError(405, "方法禁止执行");
			return;
		}
		try {
			Method m = this.getClass().getDeclaredMethod(path, HttpServletRequest.class, HttpServletResponse.class);
			Object o = m.invoke(this, request, response);
			if (o != null) {
				String str = JSON.toJSONStringWithDateFormat(o, "yyyy-MM-dd", SerializerFeature.WriteNullStringAsEmpty,
						SerializerFeature.WriteMapNullValue);
				response.getWriter().print(str);
			}
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
			response.sendError(500, "方法没有找到");
		} catch (Exception e) {
			e.printStackTrace();
			response.sendError(500, "执行出错");
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}